package cn.iocoder.yudao.module.blog.controller.app.post;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.postlike.vo.BlogPostLikeCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.postlike.vo.BlogPostLikePageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.spacelike.vo.BlogSpaceLikeCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.spacelike.vo.BlogSpaceLikePageReqVO;
import cn.iocoder.yudao.module.blog.controller.app.post.likevo.AppBlogPostLikeListReqVO;
import cn.iocoder.yudao.module.blog.controller.app.post.likevo.AppBlogPostLikeRespVO;
import cn.iocoder.yudao.module.blog.controller.app.space.likevo.AppBlogSpaceLikeListReqVO;
import cn.iocoder.yudao.module.blog.controller.app.space.likevo.AppBlogSpaceLikeRespVO;
import cn.iocoder.yudao.module.blog.convert.postlike.BlogPostLikeConvert;
import cn.iocoder.yudao.module.blog.convert.spacelike.BlogSpaceLikeConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.postlike.BlogPostLikeDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacelike.BlogSpaceLikeDO;
import cn.iocoder.yudao.module.blog.service.post.BlogPostService;
import cn.iocoder.yudao.module.blog.service.postlike.BlogPostLikeService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 帖子点赞")
@RestController
@RequestMapping("/blog/post/like")
@Validated
public class AppBlogPostLikeController {

    @Resource
    private BlogPostLikeService blogPostLikeService;

    @Resource
    private BlogPostService blogPostService;

    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "点赞")
    public CommonResult<Long> createLike(@Valid @RequestBody BlogPostLikeCreateReqVO createReqVO) {
        Long id = blogPostLikeService.createPostLike(createReqVO);
        blogPostService.updatePostLikeNum(createReqVO.getPostId(), 1);

        return success(createReqVO.getPostId());
    }

    @GetMapping("/page")
    @Operation(summary = "获得点赞分页")
    public CommonResult<PageResult<AppBlogPostLikeRespVO>> getLikePage(@Valid BlogPostLikePageReqVO pageVO) {
        PageResult<BlogPostLikeDO> pageResult = blogPostLikeService.getPostLikePage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogPostLikeDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogPostLikeConvert.INSTANCE.convertAppPage(pageResult, memberMap));
    }

    @PostMapping("/list")
    @Operation(summary = "获得文章点赞列表")
    public CommonResult<List<Long>> getLikeList(@Valid @RequestBody AppBlogPostLikeListReqVO reqVO){
        List<BlogPostLikeDO> list = blogPostLikeService.getPostLikeList(reqVO.getMemberId(), reqVO.getPostList());
        return success(convertList(list, BlogPostLikeDO::getPostId));
    }
}
